package com.leanyuan.main.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leanyuan.main.domain.Building;
import com.leanyuan.main.domain.RepForm;
import com.leanyuan.main.domain.RepReg;
import com.leanyuan.main.domain.StaffMsg;
import com.leanyuan.main.service.BuildingService_M;
import com.leanyuan.main.service.RepFormService_M;
import com.leanyuan.main.service.RepRegService_M;
import com.leanyuan.main.service.StaffMsgService_M;
import com.leanyuan.main.util.RepformVo;
import com.leanyuan.main.util.UpdateStaffIdDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 相亲相爱一家人
 * @since 2021-05-25
 */
@RestController
@RequestMapping("/leanyuan/rep-form")
public class RepFormController {
    @Autowired
    private RepFormService_M repFormServiceM;
    @Autowired
    private BuildingService_M buildingServiceM;
    @Autowired
    private StaffMsgService_M staffMsgServiceM;
    @Autowired
    private RepRegService_M repRegServiceM;


    //显示报修设置信息
    @RequestMapping("findForm")
    public RepformVo findPage(@RequestParam(defaultValue = "1") Integer pageNum) {
        Page<RepForm> page = new Page<>(pageNum, 10);
        QueryWrapper<RepForm> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        IPage<RepForm> pageData = repFormServiceM.page(page, wrapper);
        return RepformVo.success(pageData);
    }

    //添加报修位置Building和House表信息
    @GetMapping("findHouse")
    public RepformVo findHouse() {
        List<Building> build = buildingServiceM.findBuild();
        return RepformVo.success(build);
    }

    //添加或修改报修单
    @PostMapping("addForm")
    public RepformVo addForm(@RequestBody RepForm repForm) {
        System.out.println(repForm.getCreateTime());

        repFormServiceM.saveOrUpdate(repForm);
        return RepformVo.success(null);
    }



    //删除
    @DeleteMapping("delete/{id}")
    public RepformVo deleteForm(@PathVariable(name = "id")Long id) {
        QueryWrapper<RepReg> wrapper = new QueryWrapper<>();
        wrapper.eq("form_id",id);
        RepReg one = repRegServiceM.getOne(wrapper);
        if(one==null){
            boolean b = repFormServiceM.removeById(id.intValue());
            return RepformVo.success(b);
        }else {
            return RepformVo.fail("该报修单正在处理中!");
        }
    }

    //显示当前报修单维修师傅
    @GetMapping("findStaff/{id}")
    public RepformVo findStaff(@PathVariable(name = "id")Integer id) {
        QueryWrapper<StaffMsg> wrapper=new QueryWrapper<>();
        RepForm repForm = repFormServiceM.getById(id.intValue());
        if(repForm.getStaffId()!=null) {
            for (String s : repForm.getStaffId().split(",")) {
                wrapper.or().eq("staff_id", s);
            }
            List<StaffMsg> list = staffMsgServiceM.list(wrapper);
            return RepformVo.success(list);
        }else {
            return RepformVo.fail("请添加维修人员");
        }
    }

    //添加报修单师傅信息
    @GetMapping("findStaff2")
    public RepformVo findStaff2(Integer id) {
        QueryWrapper<StaffMsg> wrapper=new QueryWrapper<>();
        RepForm repForm = repFormServiceM.getById(id);
        if(repForm.getStaffId()!=null) {
            for (String s : repForm.getStaffId().split(",")) {
                wrapper.notIn("staff_id", s);
            }
        }
        wrapper.eq("p_id",1);
        List<StaffMsg> list = staffMsgServiceM.list(wrapper);
        return RepformVo.success(list);
    }

    //删除报修单维修人员(修改)
    @PostMapping("updateStaffId")
    public RepformVo updateStaffId(@RequestBody UpdateStaffIdDto dto) {
        RepForm repForm = repFormServiceM.getById(dto.getFormId());
        QueryWrapper<RepForm> wrapper=new QueryWrapper<>();
        wrapper.eq("form_id",dto.getFormId());
        repForm.setStaffId(dto.getStaffId());
        repFormServiceM.update(repForm,wrapper);
        return RepformVo.success(null);
    }

}

